<template>
  <div class="snow-page">
    <div class="snow-inner-page">
      <a-form :model="formData.form" :label-col-props="{ span: 5 }" :wrapper-col-props="{ span: 18 }" ref="formRef">
        <a-row :gutter="16">
          <a-col :span="6">
            <a-form-item field="name" label="名称">
              <a-input v-model="formData.form.name" placeholder="请输入名称" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-space>
              <a-button type="primary" @click="onSearch">
                <template #icon>
                  <icon-search />
                </template>
                <template #default>查询</template>
              </a-button>
              <a-button @click="onReset">
                <template #icon>
                  <icon-refresh />
                </template>
                <template #default>重置</template>
              </a-button>
              <a-button type="primary" @click="onAdd">
                <template #icon>
                  <icon-PlusOutlined />
                </template>
                <template #default>新增</template>
              </a-button>
            </a-space>
          </a-col>
        </a-row>
      </a-form>
      <a-table :data="tableData" row-key="id" size="small" bordered :pagination="false">
        <template #columns>
          <a-table-column title="企业编码" :width="120">
            <template #cell="{ record }">
              {{ record.cq_company_code }}
            </template>
          </a-table-column>
          <a-table-column title="企业名称/简称" :width="300">
            <template #cell="{ record }">
              {{ record.name?record.name:record.cq_company_short_name }}
            </template>
          </a-table-column>
          <a-table-column title="企业工商注册地址" :width="300">
            <template #cell="{ record }">
              {{ record.cq_address_registry }}
            </template>
          </a-table-column>
          <a-table-column title="企业法定代表人" :width="140">
            <template #cell="{ record }">
              {{ record.cq_representative_person }}
            </template>
          </a-table-column>
          <a-table-column title="企业负责人" :width="120">
            <template #cell="{ record }">
              {{ record.cq_responsible_person }}
            </template>
          </a-table-column>
          <a-table-column title="企业安全负责人" :width="140">
            <template #cell="{ record }">
              {{ record.cq_safety_responsible_person }}
            </template>
          </a-table-column>
          <a-table-column title="操作" align="center" fixed="right">
            <template #cell="{ record }">
              <a-space>
                <a-button size="mini" type="primary" @click="onEdit(record)">修改</a-button>
                <a-button size="mini" type="primary" @click="clickName(record)">详情</a-button>
                <a-button size="mini" type="primary" status="danger" @click="onDelete2(record)">删除</a-button>
              </a-space>
            </template>
          </a-table-column>
        </template>
      </a-table>
      <a-pagination :total="total" style="margin-top:10px;" @change="changePage" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { Modal, Message } from "@arco-design/web-vue";
import { getqiyeAPI, deletelqiyeAPI } from "@/api/modules/qiye/index";
import { useUserInfoStore } from "@/store/modules/user-info";
import { onMounted } from "vue";
const router = useRouter();
onMounted(() => {
  init();
});
async function init() {
  let res = await getqiyeAPI(formData.paged, formData.form);
  total.value = 0
  tableData.value = [];
  if (res.status == 2) {
    total.value = res.data.totalCount;
    tableData.value = res.data.data;
  }
}

const total = ref(0)

const tableData = ref([]);
const formData = reactive({
  form: {
    name: ""
  },
  paged: 1
});

const formRef = ref();
const onReset = () => {
  formRef.value.resetFields();
  init();
};

function changePage(page) {

  formData.paged = page;
  init()
}

 function onSearch() {
  init();
}

function clickName(val) {
  let stores = useUserInfoStore();
  stores.setSharedObject(val);
  router.push({ path: "/qiye/qiyeDetail" });
}

function onAdd() {
  let stores = useUserInfoStore();
  stores.removeSharedObject();
  router.push({ path: "/qiye/qiyeAdd" });
}

const onEdit = (row: any) => {
  let stores = useUserInfoStore();
  stores.setSharedObject(row);
  router.push({ path: "/qiye/qiyeAdd" });
};

const onDelete2 = (row: any) => {
  Modal.warning({
    title: "提示",
    content: "确定删除？",
    hideCancel: false,
    closable: true,
    onOk: async () => {
      try {
        let res = (await deletelqiyeAPI(row.id)) as any;
        if (res.status == 2) {
          Message.success("删除成功");
        } else {
          Message.error(res.statusMsg);
        }
        init();
      } catch { }
    }
  });
};
</script>

<style lang="scss">
.arco-modal-body {
  text-align: center !important;
}

::v-deep .arco-table-size-small .arco-table-thead .arco-table-cell {
  padding: 2px 16px !important;
  text-align: center !important;
}
</style>
